<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改实训记录')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-train-edit" th:object="${sysTrain}">
            <input name="trainId" th:field="*{trainId}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">实训学员：</label>
                    <div class="col-sm-8">
                        <input th:field="${sysTrain.student.studentName}" class="form-control" type="text" disabled>
                        <input name="studentId" th:value="${studentId}" class="form-control" type="hidden">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">费用类型：</label>
                    <div class="col-sm-8">
                        <select name="feeType" class="form-control" th:with="type=${@dict.getType('sys_fee_type')}" required>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{feeType}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12" shiro:hasAnyRoles="admin,boss">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">交费时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="feeTime" th:value="${#dates.format(sysTrain.feeTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12" shiro:hasAnyRoles="admin,boss">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">金额：</label>
                    <div class="col-sm-8">
                        <input name="money" th:field="*{money}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>

            <div class="col-xs-12">
                <div id="element1" class="form-group">
                    <label class="col-sm-3 control-label">现实训门店：</label>
                    <div class="col-sm-4">
                        <select class="city1 form-control m-b" name="city1" data-first-title="请选择">
                            <option value="">请选择</option>
                        </select>
                    </div>
                    <div class="col-sm-4">
                        <select class="store1 form-control m-b" name="storeId" data-first-title="请选择">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">到店时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="comeTime" th:value="${#dates.format(sysTrain.comeTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">是否调店：</label>
                    <div class="col-sm-8">
                        <div class="radio-box" th:each="dict : ${@dict.getType('sys_adjust_status')}">
                            <input type="radio" th:id="${'isAdjust_' + dict.dictCode}" name="isAdjust" th:value="${dict.dictValue}" th:field="*{isAdjust}" required>
                            <label th:for="${'isAdjust_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-xs-12">
                <div id="element2" class="form-group">
                    <label class="col-sm-3 control-label is-required">原实训门店：</label>
                    <div class="col-sm-4">
                        <select class="city2 form-control m-b" name="city2" data-first-title="请选择">
                            <option value="">请选择</option>
                        </select>
                    </div>
                    <div class="col-sm-4">
                        <select class="store2 form-control m-b" name="oldStoreId" data-first-title="请选择">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">调店时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="adjustTime" th:value="${#dates.format(sysTrain.adjustTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <textarea name="remark" class="form-control">[[*{remark}]]</textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">状态：</label>
                    <div class="col-sm-8">
                        <div class="radio-box" th:each="dict : ${@dict.getType('sys_train_status')}">
                            <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}" required>
                            <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12" id="lostTime" style="display: none;">
                <div class="form-group">
                    <label class="col-sm-3 control-label">流失时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="lostTime" th:value="${#dates.format(sysTrain.lostTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: jquery-cxselect-js" />
    <script th:inline="javascript">
        var userDatas = [[${@common.getStoreSelectJson()}]];
        $('#element1').cxSelect({
            selects: ['city1','store1'],
            jsonValue: 'v',
            data: userDatas
        });
        $('#element2').cxSelect({
            selects: ['city2','store2'],
            jsonValue: 'v',
            data: userDatas
        });
        function findCityByStoreV(targetV) {
            for (var i = 0; i < userDatas.length; i++) {
                var city = userDatas[i];
                if (city.s) {
                    for (var j = 0; j < city.s.length; j++) {
                        if (city.s[j].v == targetV) {
                            return city.n; // 返回城市名称
                        }
                    }
                }
            }
            return null; // 如果未找到，返回 null
        }
        // 设置默认值
        $(document).ready(function() {
            var storeId = [[${sysTrain.storeId}]];
            var oldStoreId = [[${sysTrain.oldStoreId}]];

            if (storeId) {
                var cityName = findCityByStoreV(storeId);
                // 设置 deptId 的默认值
                $('select[name="city1"]').val(cityName);

                // 手动触发 cxSelect 的联动逻辑
                $('select[name="city1"]').trigger('change');

                // 等待 cxSelect 加载完 userId 的选项后，再设置 userId 的默认值
                setTimeout(function() {
                    if (storeId) {
                        $('select[name="storeId"]').val(storeId);
                    }
                }, 100); // 100ms 延迟，确保选项加载完成
            }
            if (oldStoreId) {
                var cityName = findCityByStoreV(storeId);
                // 设置 deptId 的默认值
                $('select[name="city2"]').val(cityName);

                // 手动触发 cxSelect 的联动逻辑
                $('select[name="city2"]').trigger('change');

                // 等待 cxSelect 加载完 userId 的选项后，再设置 userId 的默认值
                setTimeout(function() {
                    if (oldStoreId) {
                        $('select[name="oldStoreId"]').val(oldStoreId);
                    }
                }, 100); // 100ms 延迟，确保选项加载完成
            }
        });

        var prefix = ctx + "system/train";
        $("#form-train-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-train-edit').serialize());
            }
        }

        $("input[name='feeTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='comeTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='adjustTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        $("input[name='lostTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        $('input[name="status"]').change(function() {
            if ($(this).val() === '3') {
                $('#lostTime').show();
            } else {
                $('#lostTime').hide();
            }
        });
        $(function () {
            if([[${sysTrain.status}]]=="3")
                $('#lostTime').show();
            else
                $('#lostTime').hide();

        })
    </script>
</body>
</html>